﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Setl
{
    /// <summary>
    /// 发票使用记录
    /// </summary>
    public class InvUsedRecService : SetlAppService, IInvUsedRecService
    {
        private IRepository<InvUsedRec, Guid> _invUsedRecRepository;

        public InvUsedRecService(IRepository<InvUsedRec, Guid> invUsedRecRepository)
        {
            _invUsedRecRepository = invUsedRecRepository;
        }

        /// <summary>
        /// 提交发票
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<InvUsedRecDto> PostAsync(InvUsedRecCreateDto input)
        {
            InvUsedRec invUsedRec = new InvUsedRec(input.OrdNo, input.OrdBizSouc);
            invUsedRec = await _invUsedRecRepository.InsertAsync(invUsedRec);
            var itemDto = ObjectMapper.Map<InvUsedRec, InvUsedRecDto>(invUsedRec);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<InvUsedRecDto>> GetListAsync(InvUsedRecInput input)
        {
            var iQuery = await _invUsedRecRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<InvUsedRec>, List<InvUsedRecDto>>(iQuery.ToList());

                return new PagedResultDto<InvUsedRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<InvUsedRec>, List<InvUsedRecDto>>(items.ToList());

                return new PagedResultDto<InvUsedRecDto>(totalCount, itemsDto);
            }
        }
    }
}
